package com.chixing.server.cron;

import com.chixing.mapper.PraisedMapper;
import com.chixing.pojo.Evaluate;
import com.chixing.pojo.Praised;
import com.chixing.util.DateUtil;
import com.chixing.util.redis.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 马博
 * * @date 2021/11/4
 */
@Service
public class PraisedContentSend {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private PraisedMapper praisedMapper;

    @Scheduled(cron = "*/20 * * * * ?")
    public void sendPraisedCount(){
        //监听评论表中的点赞数量
        Map<String,String> map1=(Map)stringRedisTemplate.opsForHash().entries(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT);

        Set<Map.Entry<String, String>> entries = map1.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){

            Map.Entry<String,String> stringEntry = iterator.next();
            Query query=new Query(Criteria.where("evaluateId").is(Integer.valueOf(stringEntry.getKey())));
            Evaluate evaluate=mongoTemplate.findOne(query,Evaluate.class);
            System.out.println("持久化到数据库中的evaluateId对应的数据是:"+evaluate);
            assert evaluate != null;
            evaluate.setEvaluatePraisedCount(Integer.valueOf(stringEntry.getValue()));
            System.out.println("持久化到数据库的点赞量"+evaluate.getEvaluatePraisedCount());
            Update update=Update.update("evaluatePraisedCount",evaluate.getEvaluatePraisedCount());
            mongoTemplate.updateFirst(query,update,Evaluate.class);
        }
        stringRedisTemplate.delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT);

        //用来监听点赞表中的数据
        Map<String,String> map2=(Map)stringRedisTemplate.opsForHash().entries(RedisKeyUtils.MAP_KEY_USER_LIKED);
        Set<Map.Entry<String,String>> stringStringSet = map2.entrySet();
        Iterator<Map.Entry<String,String>> entryIterator = stringStringSet.iterator();
        while(entryIterator.hasNext()){
            Praised praised = new Praised();
            Map.Entry<String,String> stringEntry = entryIterator.next();
            String [] keyStrings = stringEntry.getKey().split("::");
            String [] valueStrings = stringEntry.getValue().split("::");
            Evaluate evaluateExm = new Evaluate();
            evaluateExm.setEvaluateId(Integer.valueOf(keyStrings[0]));
            Query query = new Query(Criteria.byExample(evaluateExm));
            // 从mongodb中查出数据
            Evaluate evaluate=mongoTemplate.findOne(query,Evaluate.class);
            Praised praisedExm = new Praised();
            assert evaluate != null;
            praisedExm.setEvaluateId(evaluate.getEvaluateId());
            praisedExm.setCustId(Integer.valueOf(keyStrings[1]));

            Query query1 = new Query(Criteria.byExample(praisedExm));
            Praised praised1 = mongoTemplate.findOne(query1,Praised.class);
            if(praised1 == null){
            System.out.println(evaluate);
            praised.setEvaluateId(evaluate.getEvaluateId());
            praised.setCustId(Integer.valueOf(keyStrings[1]));
            praised.setPrisedTime(DateUtil.stringToDate(valueStrings[1],DateUtil.DATE_PATTEN_YMDHMS));
            praised.setStatus(Integer.valueOf(valueStrings[0]));
            mongoTemplate.save(praised);
            }else{
                Update update = Update.update("status",Integer.valueOf((valueStrings[0])));
                mongoTemplate.updateFirst(query1,update,Praised.class);
            }
        }
        stringRedisTemplate.delete(RedisKeyUtils.MAP_KEY_USER_LIKED);
    }
}
